package cn.hiapi.shopkeeper.entity.order;

import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.serializer.LongToStringSerializer;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.enums.OrderStatus;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.math.BigDecimal;

/**
 * @author AdinZ
 * @date 2022/12/16 23:59
 */
@Entity
@Table(name = "hiapi_shopkeeper_activity_order")
@ApiModel(description = "活动订单")
@Getter
@Setter
public class ActivityOrder extends BasicEntity {
    @Id
    @GeneratedValue(generator = "snowflake")
    @GenericGenerator(name = "snowflake", strategy = "cn.hiapi.basic.id.SnowflakeId")
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private Long id;

    @ApiModelProperty("店铺id")
    private long shopId;

    @ApiModelProperty("买家id")
    private long buyerId;

    @ApiModelProperty("卖家id")
    private long sellerId;

    @ApiModelProperty("活动id")
    private long aid;

    @ApiModelProperty("商品名")
    private String title;

    @ApiModelProperty("商品图片")
    private String thumb;

    @ApiModelProperty("活动类型")
    @Enumerated(EnumType.STRING)
    private ActivityType activityType;

    @ApiModelProperty("付款信息")
    private String payType;

    @ApiModelProperty("实际付款金额,totalMoney-discount-sugar")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal payment = BigDecimal.ZERO;

    @ApiModelProperty("交易状态,0 未付款,1已付款")
    private int statusType;

    @ApiModelProperty("订单类型 0 待确认,1已成功 用于统计")
    private int orderType;

    @ApiModelProperty("喜糖抵扣数量")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal sugar = BigDecimal.ZERO;

    @ApiModelProperty("优惠金额 用作喜糖抵扣")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal discount = BigDecimal.ZERO;

    @ApiModelProperty("商品原价格")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal costPrice = BigDecimal.ZERO;
    @ApiModelProperty("接龙折扣返现")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal solitaire = BigDecimal.ZERO;

    @ApiModelProperty("单价")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal price = BigDecimal.ZERO;

    @ApiModelProperty("购买数量")
    private int number = 1;

    @ApiModelProperty("应付总额 price * number")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal totalMoney = BigDecimal.ZERO;

    @ApiModelProperty("订单状态")
    @Enumerated(EnumType.STRING)
    private OrderStatus status = OrderStatus.WAIT_PAY;

    @ApiModelProperty("使用状态 0未使用,1已使用")
    private int useStatus;

    @ApiModelProperty("创建时间")
    private long created = System.currentTimeMillis();

    /**
     * 订单超时不支付关闭订单
     */
    @ApiModelProperty("超时支付关闭时间")
    private long expire;


    @ApiModelProperty("支付时间")
    private Long payTime;


    @ApiModelProperty("更新时间")
    private long updated = System.currentTimeMillis();

    @ApiModelProperty("买家订单备注")
    private String buyerRemark;

    /**
     * 外链类型
     * activityType==GROUP
     * new 新团购, join 加入的团购, cost 原价购买
     */
    private String fidType;

    /**
     * 外链id  如团购的 id
     * {@link ActivityGroupOrder#getId()}
     */
    @ApiModelProperty("外链id")
    private Long fid;

    @ApiModelProperty("兑换开始时间")
    private long useStart;

    @ApiModelProperty("兑换结束时间")
    private long useEnd;


    @ApiModelProperty("推荐人 ")
    private Long inviteUid;

    @ApiModelProperty("团购状态,0 没有入团,1已入团")
    private int groupStatus;

    @ApiModelProperty("其他数据")
    private String otherText;

    @Column(columnDefinition = "text")
    @ApiModelProperty("扩展活动资料")
    private String expandText;


    @ApiModelProperty("删除状态 1已删除")
    private int delStatus;


    @ApiModelProperty("转移方向 0 没转增, 1转出,2转入")
    private int giftDirection;

    @ApiModelProperty("转给,转入 用户id")
    private Long giftUid;

    private Long useTime;

    /**
     * 计算支付金额
     */
    public void calculate() {
        if (this.price == null) this.price = BigDecimal.ZERO;
        this.totalMoney = this.price.multiply(BigDecimal.valueOf(this.number));
        this.payment = this.totalMoney.subtract(this.discount);
        if (this.payment.compareTo(BigDecimal.ZERO) < 0) this.payment = BigDecimal.ZERO;
    }

}
